package com.yuan.zsschool.ui.classManage;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yuan.base.framework.BaseFrameworkActivity;
import com.yuan.base.framework.BaseViewHolderAdapter;
import com.yuan.base.framework.bean.TeachSchool;
import com.yuan.base.framework.bean.TipResult;
import com.yuan.base.framework.http.BaseHttpCallBack;
import com.yuan.base.framework.http.BaseHttpRequest;
import com.yuan.base.utils.DialogUtil;
import com.yuan.base.widget.AutoLoadList;
import com.yuan.zsschool.AppContext;
import com.yuan.zsschool.R;
import com.yuan.zsschool.Urls;
import com.yuan.zsschool.bean.Grade;
import com.yuan.zsschool.ui.widget.SelectSchool;

import java.util.HashMap;
import java.util.List;

/**
 * 班级管理
 */
public class ClassManageActivity extends BaseFrameworkActivity {


	// --------------------------控件--------------------------
	private AutoLoadList auto_load_list;
	private ListView data_list;// 下拉刷新
	private SelectSchool ss_select_school;
	// --------------------------变量--------------------------
	private List<Grade> dataList;
	private WaitAdapter waitAdapter;

	@Override
	protected int getLayoutId() {
		return R.layout.activity_class_manage;
	}

	@Override
	protected void bindViews() {
		auto_load_list = findViewById(R.id.auto_load_list);
		ss_select_school = findViewById(R.id.ss_select_school);
		auto_load_list.allowLoadMore(false);
		data_list = auto_load_list.getData_list();

		ss_select_school.init(AppContext.getPermission(), teachSchool -> auto_load_list.startRefreshing());
		auto_load_list.setOnRefreshingListener(new AutoLoadList.OnRefreshingListener() {
			@Override
			public void onRefresh() {
				getListData();
			}

			@Override
			public void onLoadMore() {
			}
		});
		setOnClickListener(R.id.fl_add, v -> {
			DialogUtil.input(ClassManageActivity.this, "新增班级", "", "确认", (dialog, content, userName) -> {
				createGrade(1, content);
			}, "取消", null);
		});

	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		List<TeachSchool> teachSchoolList = AppContext.getPermission();
		if (teachSchoolList != null && !teachSchoolList.isEmpty()) {
			auto_load_list.startRefreshing();
		}else{
			showToast("无任教学校");
			closeSelf();
		}
	}

	private void getListData() {
		HashMap<String, Object> params = new HashMap<>();
		params.put("school", ss_select_school.getCurrentSchoolId());
		params.put("page", 0);
		params.put("size", 999);
		BaseHttpRequest.get(Urls.getUrl(Urls.getGradeById), params, new BaseHttpCallBack<JSONObject>() {

			@Override
			public void onFailure(int code, String msg) {
				super.onFailure(code, msg);
				auto_load_list.completeRefresh();
			}

			@Override
			public void onPost() {
				auto_load_list.completeRefresh();
			}

			@Override
			public void onSuccess(JSONObject result) {
				if (result != null && result.containsKey("content")) {
					List<Grade> noticeList = JSON.parseArray(result.getString("content"), Grade.class);
					dataList = noticeList;
					init(true);
				}
			}

		});
	}

	private void init(boolean needNew) {
		if (needNew) {
			waitAdapter = new WaitAdapter(this);
			waitAdapter.setData(dataList);
			data_list.setAdapter(waitAdapter);
		} else {
			waitAdapter.setData(dataList);
			waitAdapter.notifyDataSetChanged();
		}
	}

	private class WaitAdapter extends BaseViewHolderAdapter<Grade, Holder> {

		private WaitAdapter(Context context) {
			super(context);
		}

		@Override
		protected void bindViews(View convertView, Holder holder, Grade item) {
			holder.tv_name = convertView.findViewById(R.id.tv_name);
			holder.btn_change_name = convertView.findViewById(R.id.btn_change_name);
			holder.btn_delete = convertView.findViewById(R.id.btn_delete);
		}

		@Override
		protected View getConvertView(Grade item, LayoutInflater inflater, int position, ViewGroup parent) {
			return inflater.inflate(R.layout.item_class_list, parent, false);
		}

		@Override
		protected Holder getHolder() {
			return new Holder();
		}

		@Override
		protected void refreshView(int position, Grade item, View convertView, Holder holder) {
			holder.tv_name.setText(item.getName());
			holder.btn_change_name.setOnClickListener(v -> {
				DialogUtil.input(ClassManageActivity.this, "更名", item.getName(), "确认", (dialog, content, userName) -> {
					changeName(item.getId(), content);
				}, "取消", null);
			});
			holder.btn_delete.setOnClickListener(v -> {
				DialogUtil.confirm(ClassManageActivity.this, "提示", "是否确认删除？", "确认", (dialog, which) -> {
					delete(item.getId());
				}, "取消", null);
			});
		}
	}

	private void delete(int id) {
		String url = Urls.getUrl(Urls.deleteGrade);
		HashMap<String, Object> params = new HashMap<>();
		params.put("classId", id);
		BaseHttpRequest.post(url, params, new BaseHttpCallBack<TipResult>() {

			@Override
			public void onSuccess(TipResult result) {
				if (result.isSuccess()) {
					showToast("提交成功");
				} else {
					showToast(result.getMessage());
				}
				auto_load_list.startRefreshing();
			}
		});
	}

	private void changeName(int id, String name) {
		String url = Urls.getUrl(Urls.changeGradeName);
		HashMap<String, Object> params = new HashMap<>();
		params.put("classId", id);
		params.put("className", name);
		BaseHttpRequest.post(url, params, new BaseHttpCallBack<TipResult>() {

			@Override
			public void onSuccess(TipResult result) {
				if (result.isSuccess()) {
					showToast("提交成功");
				} else {
					showToast(result.getMessage());
				}
				auto_load_list.startRefreshing();
			}
		});
	}

	private void createGrade(int id, String name) {
		String url = Urls.getUrl(Urls.addGrade);
		HashMap<String, Object> params = new HashMap<>();
		params.put("school", id);
		params.put("className", name);
		BaseHttpRequest.post(url, params, new BaseHttpCallBack<TipResult>() {

			@Override
			public void onSuccess(TipResult result) {
				if (result.isSuccess()) {
					showToast("提交成功");
				} else {
					showToast(result.getMessage());
				}
				auto_load_list.startRefreshing();
			}
		});
	}

	private class Holder {
		TextView tv_name;
		Button btn_change_name;
		Button btn_delete;
	}
}
